package com.cjh.controller;


import com.cjh.entity.Person;
import com.cjh.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjh
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/getPort")
    public String getPort(){
//        int i=1/0;
        return port;
    }

    @GetMapping("/list")
    public List<Person> list(){

        List<Person> personList = personService.list();
        return personList;
    }

    @GetMapping("/get/{id}")
    public Person get(@PathVariable("id") String id){

        return personService.getById(id);
    }

    @GetMapping("/getByName/{name}")
    public Person getByName(@PathVariable("name") String name){

        return personService.getByName(name);
    }

    @PostMapping("/add")
    public String add(Person person){

        boolean flag = personService.save(person);
        if(flag){
            return "SUCCESS";
        }
        return "ERROR";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id){

        boolean flag = personService.removeById(id);
        if(flag){
            return "SUCCESS";
        }
        return "ERROR";
    }

    @PutMapping("/update")
    public String update(Person person){

        boolean flag = personService.updateById(person);
        if(flag){
            return "SUCCESS";
        }
        return "ERROR";
    }
}

